#pragma once
#include "ZuiLib.h"
#include "LUtils.h"

namespace ZuiLib {

class LWindow :public LuaClass<ZWindow>
{
public:
	void ctor(LuaFuncState& L)
	{
		pThis = new ZLuaWindow(L);
	}

	void dtor()
	{
		delete pThis;
	}

	int createWindow(LuaFuncState& L)
	{
		bool rslt=pThis->CreateWindow(L.arg(0).toString(), L.arg(1).toInt(), L.arg(2).toInt(), L.arg(3).toInt(),L.arg(4).toInt());
		return L.lreturn(rslt);
	}

	int setWindowTitle(LuaFuncState& L)
	{
		bool rslt = pThis->SetWindowTitle(L.arg(0).toString());
		return L.lreturn(rslt);
	}

	int getWindowTitle(LuaFuncState& L)
	{
		return L.lreturn(pThis->GetWindowTitle());
	}

	int loadLayout(LuaFuncState& L) 
	{
		const char* file = L.arg(0).toString();

		ZPaintManager * mgr = pThis->GetManager();
		ZBuilder builder;
		if (builder.LoadXml(file))
		{
			bool isOk = false;
			ZControl* ctl = builder.Create(mgr);
			if (mgr->AttachDialog(ctl))
				isOk = true;
			else
				delete ctl;
			return L.lreturn(isOk);
		}
		else
		{
			return L.error("cant load %s err:%s", file,builder.GetErrorInfo());
		}
	}


	int setPosition(LuaFuncState& L)
	{
		pThis->SetPosition(toPoint(L.arg(0)));
		return 0;
	}


	int getPosition(LuaFuncState& L)
	{
		return L.lreturn(toLua(&L, pThis->GetPosition()));
	}

	int setSize(LuaFuncState& L)
	{
		pThis->SetSize(toSize(L.arg(0)));
		return 0;
	}


	int getSize(LuaFuncState& L)
	{
		return L.lreturn(toLua(&L, pThis->GetSize()));
	}

	int setMinimumSize(LuaFuncState& L)
	{
		pThis->SetMinimumSize(toSize(L.arg(0)));
		return 0;
	}


	int getMinimumSize(LuaFuncState& L)
	{
		return L.lreturn(toLua(&L, pThis->GetMinimumSize()));
	}

	int setMaximumSize(LuaFuncState& L)
	{
		pThis->SetMaximumSize(toSize(L.arg(0)));
		return 0;
	}


	int getMaximumSize(LuaFuncState& L)
	{
		return L.lreturn(toLua(&L, pThis->GetMaximumSize()));
	}

	int showWindow(LuaFuncState& L)
	{
		pThis->ShowWindow(L.arg(0).toBool());
		return 0;
	}

	int raiseWindow(LuaFuncState& L)
	{
		pThis->RaiseWindow();
		return 0;
	}

	int setMaximize(LuaFuncState& L)
	{
		pThis->SetMaximize();
		return 0;
	}

	int setMinimize(LuaFuncState& L)
	{
		pThis->SetMinimize();
		return 0;
	}

	int setRestore(LuaFuncState& L)
	{
		pThis->SetRestore();
		return 0;
	}

	int setGrab(LuaFuncState& L)
	{
		pThis->SetGrab(L.arg(0).toBool());
		return 0;
	}

	int getGrab(LuaFuncState& L)
	{
		return L.lreturn(pThis->GetGrab());
	}

	int setBrightness(LuaFuncState& L)
	{
		pThis->SetBrightness(L.arg(0).toNumber());
		return 0;
	}

	int getBrightness(LuaFuncState& L)
	{
		return L.lreturn(pThis->GetBrightness());
	}
	int destroy(LuaFuncState& L)
	{
		pThis->Destroy();
		return 0;
	}

    int close(LuaFuncState& L)
    {
        pThis->Destroy();
        return 0;
    }

	int isFocus(LuaFuncState& L)
	{
		return L.lreturn(pThis->IsFocus());
	}

	int getDpi(LuaFuncState& L)
	{
		return L.lreturn(pThis->GetDpi());
	}

	int setCursor(LuaFuncState& L)
	{
		pThis->SetCursor((SDL_SystemCursor)L.arg(0).toUInt());
		return 0;
	}

	int isMaximized(LuaFuncState& L)
	{
		return L.lreturn(pThis->IsMaximized());
	}

	int findControl(LuaFuncState& L)
	{
		ZControl* ctrl = pThis->GetManager()->FindControl(L.arg(0).toString());
		if (ctrl)
			return L.lreturn(LuaClass<ZControl>::cToLua(&L, ctrl));
		else
			return 0;
	}

    int getScale(LuaFuncState& L)
    {
        return L.lreturn(ZDpi::Instance()->GetScaleValue());
    }
    

	BEGIN_MAP_FUNC(LWindow)
	DECLARE_FUNC(createWindow),
	DECLARE_FUNC(setWindowTitle),
	DECLARE_FUNC(getWindowTitle),
	DECLARE_FUNC(loadLayout),
	DECLARE_FUNC(setPosition),
	DECLARE_FUNC(getPosition),
	DECLARE_FUNC(setSize),
	DECLARE_FUNC(getSize),
	DECLARE_FUNC(setMinimumSize),
	DECLARE_FUNC(getMinimumSize),
	DECLARE_FUNC(setMaximumSize),
	DECLARE_FUNC(getMaximumSize),
	DECLARE_FUNC(showWindow),
	DECLARE_FUNC(raiseWindow),
	DECLARE_FUNC(setMaximize),
	DECLARE_FUNC(setMinimize),
	DECLARE_FUNC(setRestore),
	DECLARE_FUNC(setGrab),
	DECLARE_FUNC(getGrab),
	DECLARE_FUNC(setBrightness),
	DECLARE_FUNC(getBrightness),
	DECLARE_FUNC(destroy),
    DECLARE_FUNC(close),
	DECLARE_FUNC(isFocus),
	DECLARE_FUNC(getDpi),
	DECLARE_FUNC(setCursor),
	DECLARE_FUNC(isMaximized),
	DECLARE_FUNC(findControl),
    DECLARE_FUNC(getScale),
	END_MAP_FUNC


	BEGIN_MAP_CONST
	DECLARE_CONST("POS_CENTER", ZWindow::WINDOW_POS_CENTER),

	DECLARE_CONST("FLG_FULLSCREEN", SDL_WINDOW_FULLSCREEN),
	DECLARE_CONST("FLG_OPENGL", SDL_WINDOW_OPENGL),
	DECLARE_CONST("FLG_SHOWN", SDL_WINDOW_SHOWN),
	DECLARE_CONST("FLG_HIDDEN", SDL_WINDOW_HIDDEN),
	DECLARE_CONST("FLG_TOPMOST", SDL_WINDOW_ALWAYS_ON_TOP),
	DECLARE_CONST("FLG_SKIP_TASKBAR", SDL_WINDOW_SKIP_TASKBAR),
	DECLARE_CONST("FLG_MOUSE_CAPTURE", SDL_WINDOW_MOUSE_CAPTURE),
	DECLARE_CONST("FLG_TOOLTIP", SDL_WINDOW_TOOLTIP),
	DECLARE_CONST("FLG_POPUP_MENU", SDL_WINDOW_POPUP_MENU),


	DECLARE_CONST("CURSOR_ARROW", SDL_SYSTEM_CURSOR_ARROW),
	DECLARE_CONST("CURSOR_IBEAM", SDL_SYSTEM_CURSOR_IBEAM),
	DECLARE_CONST("CURSOR_WAIT", SDL_SYSTEM_CURSOR_WAIT),
	DECLARE_CONST("CURSOR_CROSSHAIR", SDL_SYSTEM_CURSOR_CROSSHAIR),
	DECLARE_CONST("CURSOR_WAITARROW", SDL_SYSTEM_CURSOR_WAITARROW),
	DECLARE_CONST("CURSOR_SIZENWSE", SDL_SYSTEM_CURSOR_SIZENWSE),
	DECLARE_CONST("CURSOR_SIZENESW", SDL_SYSTEM_CURSOR_SIZENESW),
	DECLARE_CONST("CURSOR_SIZEWE", SDL_SYSTEM_CURSOR_SIZEWE),
	DECLARE_CONST("CURSOR_SIZENS", SDL_SYSTEM_CURSOR_SIZENS),
	DECLARE_CONST("CURSOR_SIZEALL", SDL_SYSTEM_CURSOR_SIZEALL),
	DECLARE_CONST("CURSOR_NO", SDL_SYSTEM_CURSOR_NO),
	DECLARE_CONST("CURSOR_HAND", SDL_SYSTEM_CURSOR_HAND),
	END_MAP_CONST
};


}//namespace

